package com.cx.mall.demo.controller;


import com.cx.mall.common.utils.ResponseVO;
import com.cx.mall.demo.model.UserInfo;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController//加了这个注解就成了控制器
@RequestMapping("/test")
public class HelloContorller {
    @GetMapping("/hello")
    public ResponseVO<String> hello() {

        return ResponseVO.success("Hello World");
    }

    @GetMapping("/name")
    public ResponseVO<String> hello1(String name) {
        return ResponseVO.success(name);
    }

    @GetMapping("/get")
    public ResponseVO<String> hello2(
            @RequestParam(value = "userName",defaultValue = "测试") String name,
            @RequestParam(required = true) Integer age,
            String sex) {
        String info = "name:" + name + "age:" + age + "sex:" + sex;
        return ResponseVO.success(info);
    }

    @GetMapping("/getArray")
    public ResponseVO getArray(
            String name,
            @RequestParam(value = "love") String[] loves) {
        Map<String, Object> info = new HashMap<>();
        info.put("name",name);
        info.put("loves",Arrays.asList(loves));
        return ResponseVO.success(info);
    }

    @PostMapping("/postJson")
    public ResponseVO postJson(@RequestBody UserInfo userInfo) {
        //@RequestBody: 表示接收的时json数据，将json对象转换成Java对象
        return ResponseVO.success(userInfo);
    }

}
